<script setup>
import { computed } from 'vue'

const props = defineProps({
  fault: {
    type: Object,
    required: true
  }
})

const getStatusColor = (status) => {
  return status === '异常' ? '#ff4d4f' : '#52c41a'
}
</script>

<template>
  <div class="fault-location-card">
    <div class="card-header">
      <h2><i class="fas fa-crosshairs"></i> 故障定位</h2>
      <el-tag type="warning">AI诊断结果可信度: 92.7%</el-tag>
    </div>

    <div class="location-content">
      <div class="fault-overview">
        <div class="fault-component">
          <i class="fas fa-microchip"></i> 故障部件:
          <span class="highlight">{{ fault.component }}</span>
        </div>

        <div class="diagnosis-source">
          <el-tag type="success" size="small">
            <i class="fas fa-brain"></i> AI模型诊断
          </el-tag>
          <el-tag type="info" size="small">
            <i class="fas fa-project-diagram"></i> 规则引擎验证
          </el-tag>
        </div>
      </div>

      <div class="sensor-data">
        <h3><i class="fas fa-wave-square"></i> 传感器数据</h3>

        <div class="data-grid">
          <div
              v-for="(value, key) in fault.sensorData"
              :key="key"
              class="data-item"
              :style="{ borderLeft: `4px solid ${getStatusColor(value.status)}` }"
          >
            <div class="data-name">{{ key }}</div>
            <div class="data-value">
              {{ value.value }} {{ value.unit }}
              <el-tag
                  size="small"
                  :type="value.status === '异常' ? 'danger' : 'success'"
              >
                {{ value.status }}
              </el-tag>
            </div>
            <div class="data-range" v-if="value.normalRange">
              正常范围: {{ value.normalRange[0] }} - {{ value.normalRange[1] }}{{ value.unit }}
            </div>
          </div>
        </div>
      </div>

      <div class="confidence-analysis">
        <h3><i class="fas fa-chart-line"></i> 故障可能性分析</h3>

        <div class="confidence-bars">
          <div class="confidence-item">
            <div class="confidence-label">传感器故障</div>
            <div class="confidence-bar-container">
              <div
                  class="confidence-bar"
                  :style="{ width: '85%', backgroundColor: '#ff7e5f' }"
              ></div>
            </div>
            <div class="confidence-value">85%</div>
          </div>

          <div class="confidence-item">
            <div class="confidence-label">线路问题</div>
            <div class="confidence-bar-container">
              <div
                  class="confidence-bar"
                  :style="{ width: '60%', backgroundColor: '#feb47b' }"
              ></div>
            </div>
            <div class="confidence-value">60%</div>
          </div>

          <div class="confidence-item">
            <div class="confidence-label">软件错误</div>
            <div class="confidence-bar-container">
              <div
                  class="confidence-bar"
                  :style="{ width: '30%', backgroundColor: '#a0b1d0' }"
              ></div>
            </div>
            <div class="confidence-value">30%</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.fault-location-card {
  background: linear-gradient(135deg, #1a2a52, #152245);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
  border: 1px solid #2a3a6a;
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #2a3a6a;
}

.card-header h2 {
  font-size: 18px;
  color: #e4e8f0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.location-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.fault-overview {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: rgba(20, 35, 70, 0.5);
  border-radius: 8px;
}

.fault-component {
  font-size: 16px;
}

.fault-component .highlight {
  color: #ff7e5f;
  font-weight: 600;
}

.diagnosis-source {
  display: flex;
  gap: 10px;
}

.sensor-data h3, .confidence-analysis h3 {
  font-size: 16px;
  color: #e4e8f0;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.data-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}

.data-item {
  background: rgba(10, 25, 55, 0.7);
  border-radius: 8px;
  padding: 15px;
  transition: all 0.3s;
}

.data-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.data-name {
  text-transform: capitalize;
  font-weight: 600;
  margin-bottom: 8px;
  color: #a0b1d0;
}

.data-value {
  font-size: 20px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
}

.data-range {
  font-size: 12px;
  color: #8898b8;
}

.confidence-bars {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.confidence-item {
  display: flex;
  align-items: center;
  gap: 15px;
}

.confidence-label {
  width: 120px;
  font-size: 14px;
}

.confidence-bar-container {
  flex: 1;
  height: 20px;
  background-color: rgba(42, 58, 106, 0.5);
  border-radius: 10px;
  overflow: hidden;
}

.confidence-bar {
  height: 100%;
  border-radius: 10px;
  transition: width 1s ease;
}

.confidence-value {
  width: 50px;
  text-align: right;
  font-weight: bold;
}
</style>